const express = require('express');
const app = express();
const path = require('path');
const multer = require('multer');
const fs = require('fs')

//加载静态文件
app.use(express.static('./static'));
//multer中间件
app.use( multer({ dest: path.join(__dirname, 'uploads/') }).any() );
//监听
app.listen(8080,() => {
    console.log(' === >','running...'); 
})
//post方式文件上传
app.post('/upload',(req,res) => {
    // console.log('req.files====>',req.files)
    //旧的路径
    let oldPath = req.files[0].path;
    //extname获取.xxx
    let newPath = req.files[0].path + path.extname( req.files[0].originalname )
    //rename重命名
    fs.rename(oldPath, newPath, err => {
        if(err) console.log('err ====>>>>', err);
    })
    res.send('上传成功！！！');
})